package com.studio.julianne.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import com.studio.julianne.EMF;
import com.studio.julianne.model.Student;
import com.studio.julianne.PMF;

import javax.jdo.PersistenceManager;
import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@SuppressWarnings("serial")
@Component
@Repository
public class StudentDAOImpl implements StudentDAO {
    private static final Logger log = Logger.getLogger(StudentDAOImpl.class.getName());


	protected EntityManager entityManager;
	
	@Autowired
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public void persist(Student bean) {
		EntityManager entityManager = EMF.get().createEntityManager();
        try {
        	entityManager.persist(bean);
        } finally {
        	entityManager.close();
        }
	}

	public Collection<Student> selectAll() {
		EntityManager entityManager = EMF.get().createEntityManager();
		Collection<Student> students= null;
		try {
			students = entityManager.createQuery("SELECT s FROM com.studio.julianne.model.Student s").getResultList();
		} finally {
	    	//entityManager.close();
	    }
		return students;
	}

	public Student get(String id) {
		EntityManager entityManager = EMF.get().createEntityManager();
		try {
			return (Student) entityManager.find(Student.class, id);
	    } finally {
	    	entityManager.close();
	    }
	}

	public void delete(String id) {
		EntityManager entityManager = EMF.get().createEntityManager();
        try {
        	entityManager.remove(id);
        } finally {
        	entityManager.close();
        }
	}

}
